/**
 * 拍摄或从手机相册中选择图片或视频
 * @param option
 * @param callback
 * @returns
 */
export const chooseMedia = (
  option: {
    count: number;
    mediaType?: Array<"image" | "video">;
    sourceType?: Array<"album" | "camera">;
    maxDuration?: number;
  },
  callback: (files: { tempFilePath: string }[]) => void
) => {
  return uni.chooseMedia({
    ...option,
    success: resp => callback(resp.tempFiles),
  });
};

/**
 * 在新页面中全屏预览图片
 * @param urls
 * @param current
 */
export const previewImage = (urls: string[], current = 0) => {
  return uni.previewImage({ urls, current });
};

/**
 * 批量上传图片/视频
 * @param fileList
 * @param callback
 * @param remote
 */
export const uploadWrapper = (
  fileList: string[],
  callback: (resourceList: string[]) => void,
  remote: (path: string) => Promise<string[]>
) => {
  const resourceList: string[] = [];

  const loopUpload = (
    fileList: string[],
    resourceList: string[],
    callback: (resourceList: string[]) => void
  ) => {
    if (fileList.length === 0) return callback(resourceList);
    remote(fileList.shift() as string)
      .then(resp => {
        resourceList.push(...resp);
        loopUpload(fileList, resourceList, callback);
      })
      .catch(() => loopUpload(fileList, resourceList, callback));
  };

  loopUpload(fileList, resourceList, callback);
};

/**
 * 下载文件资源到本地
 * @param url
 * @param callback
 * @returns
 */
export const downloadFile = (
  url: string,
  callback: (tempFilePath: string) => void
) => {
  return uni.downloadFile({
    url,
    success: resp => {
      if (resp.statusCode === 200) {
        callback(resp.tempFilePath);
      }
    },
    fail: error => {
      console.error(`downloadFile fail`, error);
    },
  });
};

/**
 * 保存图片到相册
 * @param imgList
 * @param callback
 * @param progress
 */
export const saveImageToPhotosAlbum = (
  imgList: string[],
  callback: () => void,
  progress?: (n: number) => void
) => {
  const progressCallback = (e: UniApp.OnProgressDownloadResult) => {
    progress?.(e.progress / imgList.length);
  };

  const fileList = [...imgList];
  const loopDownload = (fileList: string[], callback: () => void) => {
    if (fileList.length === 0) return callback();
    const downloadTask = downloadFile(fileList.shift() as string, filePath => {
      uni.saveImageToPhotosAlbum({
        filePath,
        success: () => {
          downloadTask.offProgressUpdate(progressCallback);
          loopDownload(fileList, callback);
        },
      });
    });
    downloadTask.onProgressUpdate(progressCallback);
  };

  loopDownload(fileList, callback);
};

/**
 * 保存视频到相册
 * @param url
 * @param callback
 * @param progress
 */
export const saveVideoToPhotosAlbum = (
  url: string,
  callback: () => void,
  progress?: (n: number) => void
) => {
  const progressCallback = (e: UniApp.OnProgressDownloadResult) => {
    progress?.(e.progress);
  };
  const downloadTask = downloadFile(url, filePath => {
    uni.saveVideoToPhotosAlbum({
      filePath,
      success: () => {
        downloadTask.offProgressUpdate(progressCallback);
        callback();
      },
    });
  });
  downloadTask.onProgressUpdate(progressCallback);
};
